package LeetCode刷题;

/**
 * @program: Java_Study
 * @author: Xiaofan
 * @createTime: 2021-09-30 15:07
 * @description: Functions of this class is
 **/
public class 螺旋举证II {
    public int[][] generateMatrix(int n) {
        int ans[][]=new int[n][n];
        boolean vis[][]=new boolean[n][n];
        int tot=n*n;
        int num=1;
        int i=0;
        int j=0;
        ans[0][0]=num;
        vis[0][0]=true;
        while(num<tot){
            while(j+1<n&&!vis[i][j+1]){
                ans[i][++j]=++num;
                vis[i][j]=true;
            }
            while(i+1<n&&!vis[i+1][j]){
                ans[++i][j]=++num;
                vis[i][j]=true;
            }
            while(j-1>=0&&!vis[i][j-1]){
                ans[i][--j]=++num;
                vis[i][j]=true;
            }
            while(i-1>=0&&!vis[i-1][j]){
                ans[--i][j]=++num;
                vis[i][j]=true;
            }
        }
        return ans;
    }
}